
#include <iostream>
#include "RechercheClient.h"
#include "NouveauClient.h"
#include <QFileDialog>

RechercheClient::RechercheClient(QWidget *parent)
: QMainWindow(parent), Ui_RechercheClient()
{
	setupUi(this);
	clients = TrueLoveClient::GetClients();
	stats = StatistiquesClient::GetStatistiquesClients();
static QList<RendezVous>rdvs = RendezVous::GetRendezVous();
	for(int i=0; i<clients.size(); i++)
	{
		TrueLoveClient client = clients.at(i);
		listWidget_Clients->addItem(QString("%1. %2 %3").arg(client.GetId()).arg(client.GetPrenom()).arg(client.GetNom()));
	}
	bt_photo->setText("");
	activeSave = false;
}

void RechercheClient::saveAllChange()
{
	saveChange();
	NouveauClient::enregistrerClientsXML(clients);
	deleteLater();
	parentWidget()->show();
}

void RechercheClient::verifHobbie(QListWidgetItem *item)
{
	QList<QListWidgetItem *> list =  listWidget_hobbies->selectedItems();
	if(list.count() > 4)
		item->setSelected(false);
		
}

void RechercheClient::choisirPhotoClient()
{
	//QString stylesheet("background-position: center; background-image: url('");
	QString photoPath = QFileDialog::getOpenFileName(this, tr("Choisir une photo de vous..."), 
			"./pics", tr("Image Files (*.png *.jpg *.bmp)"));

	//stylesheet.append(photoPath);
	//stylesheet.append("');");
	QPixmap qpm(80,100);
	qpm.load(photoPath);
	trueLoveC->SetUrlPhoto(photoPath);
	bt_photo->setIcon(QIcon(qpm));
	bt_photo->setIconSize(QSize(80,100));
	//graphicsView_photoPersonne->setStyleSheet(stylesheet);
}

void RechercheClient::adapteListe()
{
	QString test, comparaison;
	TrueLoveClient client;
	QListWidgetItem *qlwi;
	bool et = false;
	
	comparaison = le_recherche->text();
	int numLigne = listWidget_Clients->count();
	
	if(comparaison.isNull() || comparaison.isEmpty())
	{
		et = true;
	}

	for(int x = 0; x < numLigne; x++)
	{
		qlwi = listWidget_Clients->item(x);
		test = qlwi->text();
     		if(et || test.contains(comparaison, Qt::CaseInsensitive))
		{
			qlwi->setHidden(false);
		}
		else
		{
			qlwi->setHidden(true);
		}
	}
}

void RechercheClient::saveChange()
{
	trueLoveC->SetNom(le_Nom->text());
	trueLoveC->SetPrenom(le_prenom->text());
	trueLoveC->SetAdress(te_adresse->toPlainText());
	trueLoveC->SetAge(sb_age->value());
	trueLoveC->SetEmail(le_eMail->text());
	trueLoveC->SetDiffAge(sb_diffAgePersonne->value());
	QList<QString> objs;
	if(checkb_grandAmour->isChecked())
		objs.append("grandamour");
	if(checkb_amourPassionne->isChecked()) 	
		objs.append("amourpassionne");
	trueLoveC->SetObjectifs(objs);

	objs.clear();
	QListWidgetItem *qlwi;
	int size = listWidget_hobbies->count();
	for(int i = 0; i < size;i++)
	{
		qlwi = listWidget_hobbies->item(i);
		if(qlwi->isSelected())
		{
			objs.append(qlwi->text());
		}
	}
	trueLoveC->SetHobbies(objs);
	
	//Init tab2
	trueLoveC->SetSexeRecherche(comboBox_sexePersonne->currentText());
	trueLoveC->SetApparence(hs_apparenceDesir->value());	
	trueLoveC->SetCharme(hs_charmeDesir->value());	
	trueLoveC->SetHumour(hs_humourDesir->value());	
	trueLoveC->SetConversation(hs_conversationDesir->value());
	trueLoveC->SetIntelligence(hs_intelligenceDesir->value());
	trueLoveC->SetGentillesse(hs_gentillesseDesir->value());
}

void RechercheClient::closeEvent(QCloseEvent *event)
{
	retourAccueil();
}

void RechercheClient::retourAccueil()
{
	deleteLater();
	parentWidget()->show();
}

void RechercheClient::chargePage()
{
	//Save previous a implante
	if(activeSave)
		saveChange();
	else
	{
		tabWidget->setEnabled(true);
		te_adresse->setEnabled(true);
	}
	QListWidgetItem *qlwi = listWidget_Clients->selectedItems().at(0);
	QString selectText = qlwi->text();
	QStringList qsl = selectText.split(".");
	bool ok;
	int id = qsl[0].toInt(&ok,10);
	if(ok)
	{
		TrueLoveClient tlc;
		for(int i=0;ok && i < clients.size();i++)
		{
			tlc = clients.at(i);
			if(tlc.GetId() == id)
			{
				trueLoveC =(TrueLoveClient *) &(clients.at(i));
				ok = false;
			}
		}
		activeSave = true;
		//Init tab1
		te_adresse->setText(tlc.GetAdress());
		le_Nom->setText(tlc.GetNom());
		le_prenom->setText(tlc.GetPrenom());
		le_eMail->setText(tlc.GetEmail());
		le_sexe->setText(tlc.GetSexe());
		id = tlc.GetAge();
		sb_age->setValue(id);
		
		//QString stylesheet("background-position: center; background-image: url('");
		//stylesheet.append(tlc.GetUrlPhoto());
		//stylesheet.append("');");
		QPixmap qpm(80,100);
		qpm.load(tlc.GetUrlPhoto());
		bt_photo->setIcon(QIcon(qpm));
		bt_photo->setIconSize(QSize(80,100));
	//graphicsView_photoPersonne->setStyleSheet(stylesheet);
		//bt_photo->setIcon(QIcon(tlc.GetUrlPhoto()));
		//graphicsView_photoPersonne->setStyleSheet(stylesheet);
		
		QList<QString> obj = tlc.GetHobbies();
		id = obj.size();
		QListWidgetItem *qlwi;
		int size = listWidget_hobbies->count();
		for(int i = 0; i < size;i++)
		{
			qlwi = listWidget_hobbies->item(i);
			qlwi->setSelected(false);
		}
		for(int i = 0; i < size;i++)
		{
			qlwi = listWidget_hobbies->item(i);
			for(int x = 0; x < id ;x++)
			{
				if(qlwi->text() == obj[x])
				{
					qlwi->setSelected(true);
				}
			}
		}

		//Init tab2
		id = tlc.GetDiffAge();
		sb_diffAgePersonne->setValue(id);
		hs_apparenceDesir->setSliderPosition(tlc.GetApparence());	
		hs_charmeDesir->setSliderPosition(tlc.GetCharme());	
		hs_humourDesir->setSliderPosition(tlc.GetHumour());	
		hs_conversationDesir->setSliderPosition(tlc.GetConversation());
		hs_intelligenceDesir->setSliderPosition(tlc.GetIntelligence());
		hs_gentillesseDesir->setSliderPosition(tlc.GetGentillesse());
		obj = tlc.GetObjectifs();
		id = obj.size();
		if(tlc.GetSexeRecherche() == "Homme")
			comboBox_sexePersonne->setCurrentIndex(0);
		else if(tlc.GetSexeRecherche() == "Femme")
			comboBox_sexePersonne->setCurrentIndex(1);
		else
			comboBox_sexePersonne->setCurrentIndex(2);
		if(id == 2)
		{
			checkb_grandAmour->setCheckState(Qt::Checked);
			checkb_amourPassionne->setCheckState(Qt::Checked);
		}
		else if(id == 1)
		{
			if(obj[0] == "grandamour")
			{
				checkb_grandAmour->setCheckState(Qt::Checked);
				checkb_amourPassionne->setCheckState(Qt::Unchecked);
			}
			else
			{
				checkb_amourPassionne->setCheckState(Qt::Checked);
				checkb_grandAmour->setCheckState(Qt::Unchecked);
			}
		}
		else
		{
			checkb_amourPassionne->setCheckState(Qt::Unchecked);
			checkb_grandAmour->setCheckState(Qt::Unchecked);
		}

		RendezVous rdv;
		TrueLoveClient client1,client2;
		for(int x = 0; x < rdvs.size();x++)
		{
			rdv = rdvs.at(x);
			client1 = rdv.GetCompatibilite().GetClient1();
			
			client2 = rdv.GetCompatibilite().GetClient2();
			if(client1.GetId() == tlc.GetId())
				lw_rencontre->addItem(QString("%1 %2").arg(client2.GetNom()).arg(client2.GetPrenom()));
			if(client2.GetId() == tlc.GetId())
				lw_rencontre->addItem(QString("%1 %2").arg(client1.GetNom()).arg(client1.GetPrenom()));
		}


		StatistiquesClient sc;
		ok = true;
		for(int i=0;ok && i < stats.size();i++)
		{
			sc = stats.at(i);
			if(tlc.GetId() == sc.GetIdClient())
			{
				ok = false;
			}
		}
		int var = sc.GetNbTotalRendezVous() - sc.GetNbRendezVousEnAttente();
		le_nbRDV_eff->setText(QString("%1").arg(var));
		le_nbRDV_att->setText(QString("%1").arg(sc.GetNbRendezVousEnAttente()));
		lcdNum_app->display(sc.GetMoyApparence());
		lcdNum_cha->display(sc.GetMoyCharme());
		lcdNum_hum->display(sc.GetMoyHumour());
		lcdNum_int->display(sc.GetMoyIntelligence());
		lcdNum_con->display(sc.GetMoyConversation());
		lcdNum_gen->display(sc.GetMoyGentillesse());
	}
}

RechercheClient::~RechercheClient()
{

}

